<template>
	<view class="page">
		<view class="title">需要登录才能继续操作</view>
		<view class="getPhonen">
			<button class="btn" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">手机号一键登录</button>
		</view>
		<!-- <view class="inputPhonen">
			<view @click="toLogin">其他手机号登录</view>
		</view> -->
	</view>
</template>

<script>
	import {getAccessToken, getPhoneNumber} from '@/service/wxMin'
	export default {
		data() {
			return {
				skip: "",
			}
		},
		onLoad(e) {
			if(e.id){
				this.skip = e.id;
			}
		},
		onShow() {
			
		},
		methods: {
			decryptPhoneNumber(e) {
				let grant_type = 'client_credential';
				let code = e.detail.code;
				let prams = {grant_type}
				this._getAccessToken(prams, code);
			},
			/**
			 * 跳转手机号验证码登录注册页面
			 * @method toLogin
			 */
			toLogin() {
				uni.navigateTo({ url: './login?id='+this.skip }); 
			},
			skipFun(thisId){
				uni.reLaunch({url: '../profile/profile'});
			},
			// -----------------------网络请求--------------------------
			_getAccessToken(prams, code) {
				let _this = this;
				getAccessToken(JSON.stringify(prams)).then(res => {
					const data = res.data;
					// if(data.resultCode != "0000") {
					// 	uni.showModal({
					// 		showCancel: false,
					// 		content: res.data.resultMsg
					// 	});
					// }else {
					// 	let access_token = data.access_token;
					// 	let prams = {access_token, code}
					// 	_this._getPhoneNumber(prams)
					// }
					// let prams = {access_token, code}
					// _this._getPhoneNumber(prams)
					_this._getPhoneNumber(code)
				}).catch(err => {
					uni.hideLoading();
				})
			},
			_getPhoneNumber(prams) {
				let _this = this;
				getPhoneNumber(prams).then(res => {
					let data = res.data;
					if(data.code == "0000") {
						let mobile = data.phone;
						let token = data.token;
						let userInfoData = {mobile, token};
						uni.setStorage({
							key: 'userInfo',
							data: userInfoData,
							success: function(res) {
								_this.skipFun(_this.skip);
							}
						})
					}
				}).catch(err => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style>
	.page {background-color: #ffffff; height: 100vh;}
	.title {font-size: 48rpx; padding: 60rpx 32rpx 120rpx 32rpx;}
	.getPhonen {
	  box-sizing: border-box;
	  padding: 32rpx;
	}
	.btn {
	  background: -webkit-linear-gradient(left, #44f990, #36914b);
	  background: -o-linear-gradient(right, #44f990, #36914b);
	  background: -moz-linear-gradient(right, #44f990, #36914b);
	  background: linear-gradient(to right, #44f990, #36914b);
	  color: #ffffff;
	  width: 100%;
	  height: 96rpx;
	  line-height: 96rpx;
	  font-size: 32rpx;
	  border: 0;
	  border-radius: 10rpx;
		font-size: 36rpx;
	}
	.inputPhonen {
		text-align: center;
		font-size: 28rpx;
		color: #595959;
	}
</style>
